const state = {
  goodsList: uni.getStorageSync('cart') || []
}
const mutations = {
  addGoods(state, goodsData) {
    // 1. 先查找是否有旧数据
    const indexFound = state.goodsList.findIndex(item=>item.goods_id === goodsData.goods_id)
    // 根据是否存在旧数据进行不同操作
    if (indexFound === -1) {
      // 原来不存在
      state.goodsList.push(goodsData)
    }else {
      state.goodsList = state.goodsList.map((item, index)=>{
        // 如果找到当前正在添加的那个商品 数量加一
        if (index === indexFound) {
          return {...item, number: item.number+1}
        }else {
          return {...item}
        }
      })
    }
    // 数据持久化
    // console.log('数据持久化');
    // console.log(state.goodsList);
    uni.setStorageSync('cart', state.goodsList)
    // console.log('储存后读取');
    // console.log(uni.getStorageSync('cart'));
  },
  setGoodsNumber(state, payload) {
    // 希望payload 里面有两个数据 1. goods_id 2. 最新的 Number
    const {goods_id, number} = payload
    // 在数组中找到对应 index
    const indexFound = state.goodsList.findIndex(i=>i.goods_id === goods_id)
    // 通过 Index 取对象改数量
    state.goodsList[indexFound].number = number
    uni.setStorageSync('cart', state.goodsList)
  },
  removeGoods(state, goods_id){
    const indexFound = state.goodsList.findIndex(i=>i.goods_id === goods_id)
    // console.log(indexFound);
    state.goodsList.splice(indexFound, 1)
    uni.setStorageSync('cart', state.goodsList)
  },
  toggleChecked(state, goods_id) {
    // 接收 id 找到对应产品 index
    const indexFound = state.goodsList.findIndex(i=>i.goods_id === goods_id)
    // 对应的商品选中属性取反
    state.goodsList[indexFound].isChecked = !state.goodsList[indexFound].isChecked
    // 缓存更新
    uni.setStorageSync('cart', state.goodsList)
  },
  toggleAllChecked(state) {
    // 1. 判断是否全选
    const isAllChecked = state.goodsList.length > 0 && state.goodsList.every(item => item.isChecked)
    // 2. 根据状态决定接下来所有数据的选择状态
    let isChecked
    if (isAllChecked) {
      // 全部不选
      isChecked = false
    }else {
      // 全部选
      isChecked = true
    }
    // 3. 遍历数组, 设定状态
    state.goodsList.forEach(element => {
      element.isChecked = isChecked
    });
    // 4. 缓存更新
    uni.setStorageSync('cart', state.goodsList)
  },
  clearCartAfterPayment(state) {
    // 去除被买过的东西, 只有没被选中的才能留下
    state.goodsList = state.goodsList.filter(item=>!item.isChecked)
    // 缓存更新
    uni.setStorageSync('cart', state.goodsList)
  }
  // plusGoods(state, goods_id) {
  //   const indexFound = state.goodsList.findIndex(i=>i.goods_id === goods_id)
  //   state.goodsList[indexFound].number ++
  //   uni.setStorageSync('cart', state.goodsList)
  // },
  // minusGoods(state, goods_id) {
  //   const indexFound = state.goodsList.findIndex(i=>i.goods_id === goods_id)
  //   state.goodsList[indexFound].number --
  //   uni.setStorageSync('cart', state.goodsList)
  // }
}
const actions ={}
const getters = {
  // 计算品类总数(多次购买同一个算一个)
  goodsCount: state => {
    let res = 0
    state.goodsList.forEach(element => {
      if (element.isChecked) {
        res ++
      }
    });
    return res
  },
  // 是否全选
  isAllChecked: state => {
    // every 有个缺陷, 如果数组是空, 默认返回 true
    return state.goodsList.length > 0 && state.goodsList.every(item => item.isChecked)
  },
  // 产品总数量(多次购买同一个算多个)
  totalGoodsNumber: state => {
    // 每当一个商品被选中, 就将他的 number 累加起来
    let total = 0
    state.goodsList.forEach(el => {
      if (el.isChecked) {
        total+=el.number
      }
    });
    return total
  },
  // 计算全部商品总价
  totalPrice: state => {
    let total = 0
    state.goodsList.forEach(el => {
      if (el.isChecked) {
        total += el.goods_price * el.number
      }
    });
    return total
  },
  checkedGoodsList: state => state.goodsList.filter(item=>item.isChecked)
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}